package com.gluck.cases.core.block.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gluck.cases.common.utils.dal.dao.AdminUserMapper;
import com.gluck.cases.common.utils.dal.dao.CasesRecordMapper;
import com.gluck.cases.core.block.CasesRecordBlock;
import com.gluck.cases.core.modal.assemble.CasesRecordAssemble;
import com.gluck.cases.core.modal.assemble.PageInfoAssemble;
import com.gluck.cases.core.modal.dto.CasesRecordDTO;
import com.gluck.cases.core.modal.entity.AdminUser;
import com.gluck.cases.core.modal.entity.CasesRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName CasesRecordBlockImpl
 * @Desc CasesRecordBlock 实现类
 * @Author shiyongzhe
 * @Date 2022/5/28 - 10:10 下午
 **/
@Service
public class CasesRecordBlockImpl implements CasesRecordBlock {

    @Autowired
    private CasesRecordMapper casesRecordMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public int insert(CasesRecordDTO dto) {
        return casesRecordMapper.insert(CasesRecordAssemble.CasesRecordDTOConvertCasesGroup(dto));
    }

    @Override
    public int delete(Long id) {
        return casesRecordMapper.deleteByCaseId(id);
    }

    @Override
    public PageInfo<CasesRecordDTO> queryRecordsByUserId(Long userId, Integer startPage, Integer endPage) {
        PageInfo<CasesRecord> pageInfo = new PageInfo<>(PageHelper.startPage(startPage, endPage).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                casesRecordMapper.deleteByUserId(userId);
            }
        }));
        List<CasesRecordDTO> dtos = pageInfo.getList().stream().map(x -> {
            CasesRecordDTO casesRecordDTO = CasesRecordAssemble.CasesRecordConvertCasesGroupDTO(x);
            return casesRecordDTO;
        }).collect(Collectors.toList());
        // 调用公共pageinfo 组装类
        PageInfoAssemble<CasesRecordDTO> assemble = new PageInfoAssemble<CasesRecordDTO>();
        PageInfo<CasesRecordDTO> result = assemble.covertDtoPageInfo(pageInfo, dtos);
        return result;
    }

    @Override
    public PageInfo<CasesRecordDTO> queryRecordsByUsername(String username, Integer startPage, Integer endPage) {
        AdminUser adminUser = adminUserMapper.selectByUsername(username);
        PageInfo<CasesRecordDTO> dtoPageInfo = null;
        if(!Objects.isNull(adminUser)){
            dtoPageInfo = this.queryRecordsByUserId(adminUser.getId(), startPage, endPage);
        }
        return dtoPageInfo;
    }

    @Override
    public boolean batchDeleteByUserId(Long userId) {
        int delete = casesRecordMapper.deleteByUserId(userId);
        return delete > 0;
    }

    @Override
    public boolean batchDeleteByUsername(String username) {
        int delete = casesRecordMapper.deleteByUsername(username);
        return delete > 0;
    }
}
